package GUI.gameWinOld;

import game.GameBoard;
import game.GameSettingsOld;
import game.Player;

import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import GUI.CardImages;

import carddeck.Card;
import carddeck.CardDeck;

@SuppressWarnings("serial")
public class FreshCardPanel extends JPanel implements CardPanelInterface {
	private CardDeck freshCards;
	private GameBoard gameBoard;
	private JButton drawButton;
	private boolean afterdraw;
	private CardButton drawnButton;

	public FreshCardPanel(GameBoard game, CardDeck freshCards) {
		super();
		this.freshCards = freshCards;
		gameBoard = game;
		afterdraw = false;
		drawnButton = null;
		setBorder(new TitledBorder(freshCards.getCardCount() + " kart"));
		((TitledBorder) getBorder()).setTitleColor(Color.WHITE);
		setBackground(new Color(0, 77, 0));
		//setLayout(new CardHorizontalLayout());
		setVisible(true);
		setOpaque(true);

		drawButton = new JButton();
		drawButton.setBorder(BorderFactory.createEmptyBorder());
		drawButton.setContentAreaFilled(false);
		ImageIcon img;
		img = CardImages.getInstance().getII("/carddeck/conv/resizedH/back");
		drawButton.setIcon(img);
		drawButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				drawCardActionPerformed(evt);
			}
		});
		add(drawButton);
		setBackground(new Color(0, 77, 0));
		// setLayout(new CardHorizontalLayout());
	}

	public void switchToAfterdraw(Player currentPlayer, Card drawnCard) {
		drawnButton = new CardButton(currentPlayer, gameBoard, drawnCard, true,
				true);
		remove(drawButton);
		add(drawnButton);
		afterdraw = true;
		repaint();
		revalidate();
	}

	public void disableAfterdraw() {
		//remove(drawnButton);
		removeAll();
		drawnButton = null;
		add(drawButton);
		afterdraw = false;
		repaint();
		revalidate();
	}

	public boolean isAfterdraw() {
		return afterdraw;
	}

	public void drawCardActionPerformed(ActionEvent evt) {
		if (gameBoard.IsDrawing())
			gameBoard.deselectDrawing();
		else
			gameBoard.selectDrawing();
		revalidate();
	}

	@Override
	public void refresh() {
		updateBorder();
		revalidate();
	}

	public void setHighlight(boolean hl) {
		ImageIcon img;
		if (!hl) {
			img = CardImages.getInstance().getII("/carddeck/conv/resizedH/back");
			drawButton.setIcon(img);
		} else {
			img = CardImages.getInstance().getII("/carddeck/conv/resizedH/HL_B/back");
			drawButton.setIcon(img);
		}
	}
	
	public void updateBorder() {
		((TitledBorder) getBorder()).setTitle(freshCards.getCardCount()
				+ " kart");
	}
}
